package com.tedu.egweibo.controller;


import com.tedu.egweibo.common.result.JsonResult;
import com.tedu.egweibo.common.result.StatusCode;
import com.tedu.egweibo.mapper.CommentMapper;
import com.tedu.egweibo.pojo.dto.CommentDTO;
import com.tedu.egweibo.pojo.entity.Comment;
import com.tedu.egweibo.pojo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/comment")
@Api(tags = "评论模块")
@Slf4j
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    // 发布评论
    @PostMapping("/insert")
    @ApiOperation(value = "发布评论")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        log.info("发布评论，参数：{}",commentDTO);
        // 获取用户信息
        User user = (User) session.getAttribute("user");
        log.debug("用户信息：{}",user);
        if (user == null){
            // 未登录
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Comment comment=new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setUserId(user.getId());
        comment.setCreated(new Date());
        log.debug("评论信息：{}",comment);
        commentMapper.insert(comment);
        log.info("评论成功");
        return JsonResult.success();
    }

    // 获取评论列表
    @GetMapping("/selectByWeiboId")
    @ApiOperation(value = "获取评论列表")
    @ApiImplicitParam(name = "id",value = "微博id",required = true,example = "1",dataType = "Integer")
    public JsonResult selectByWeiboId(Integer id){
        return JsonResult.success(commentMapper.selectByWeiboId(id));
    }



}
